#region File Description
//-----------------------------------------------------------------------------
// AudioManager.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;


#endregion

namespace BlueGameComponent
{
    /// <summary>
    /// Component that manages audio playback for all sounds.
    /// </summary>
    public class AudioManager : GameComponent
    {
        #region Fields


        /// <summary>
        /// The singleton for this type.
        /// </summary>
        static AudioManager audioManager = null;
        public static AudioManager Instance
        {
            get
            {
                return audioManager;
            }
        }

        static public bool Switch = true;

        // Audio Data        
        Dictionary<string, SoundEffectInstance> soundBank;
        Dictionary<string, Song> musicBank;


        #endregion

        #region Initialization


        private AudioManager(Game game)
            : base(game) { }

        public static void Initialize(Game game)
        {
            audioManager = new AudioManager(game);
            audioManager.soundBank = new Dictionary<string, SoundEffectInstance>();
            audioManager.musicBank = new Dictionary<string, Song>();
            AudioManager.Switch = true;
            game.Components.Add(audioManager);
        }


        #endregion

        #region Loading Methodes
       
        public static void PlaySound(string contentName)
        {
            if (!AudioManager.Switch)
            {
                return;
            }
            if (!audioManager.soundBank.ContainsKey(contentName))
            {
                SoundEffect soundEffect = audioManager.Game.Content.Load<SoundEffect>(contentName);
                SoundEffectInstance soundEffectInstance = soundEffect.CreateInstance();
                audioManager.soundBank.Add(contentName, soundEffectInstance);
            }
            audioManager.soundBank[contentName].Play();
        }
        public static void StopSound(string soundName)
        {
            if (audioManager.soundBank.ContainsKey(soundName))
            {
                audioManager.soundBank[soundName].Stop();
            }
        }
        public static void StopSounds()
        {
            foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
            {
                if (sound.State != SoundState.Stopped)
                {
                    sound.Stop();
                }
            }
        }
        public static void PauseResumeSounds(bool resumeSounds)
        {
            SoundState state = resumeSounds ? SoundState.Paused : SoundState.Playing;

            foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
            {
                if (sound.State == state)
                {
                    if (resumeSounds)
                    {
                        sound.Resume();
                    }
                    else
                    {
                        sound.Pause();
                    }
                }
            }
        }

        public static void PlayMusic(string contentName)
        {
            if (!AudioManager.Switch)
            {
                return;
            }
            if (!audioManager.musicBank.ContainsKey(contentName))
            {
                Song song = audioManager.Game.Content.Load<Song>(contentName);
                audioManager.musicBank.Add(contentName, song);
            }

            if (!MediaPlayer.GameHasControl)
            {
                // Do not play music if the phone does not have control of the media player
                return;
            }

            if (MediaPlayer.State != MediaState.Stopped)
            {
                MediaPlayer.Stop();
            }

            try
            {
                MediaPlayer.Play(audioManager.musicBank[contentName]);

                MediaPlayer.IsRepeating = true;
            }
            catch (UnauthorizedAccessException)
            {
                // Simply do nothing. This will happen if the Zune application is launched.
            }
            catch (InvalidOperationException)
            {
                // Simply do nothing. This will happen if the Zune application is launched.
            }
        }
        public static void StopMusic()
        {
            if (!MediaPlayer.GameHasControl)
            {
                // Do not interfere with the media player if the game doesn't have control of it
                return;
            }

            if (MediaPlayer.State != MediaState.Stopped)
            {
                MediaPlayer.Stop();
            }
        }
       

        #endregion

        #region Instance Disposal Methods


        /// <summary>
        /// Clean up the component when it is disposing.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    foreach (var item in soundBank)
                    {
                        item.Value.Dispose();
                    }
                    soundBank.Clear();
                    soundBank = null;
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }


        #endregion
    }
}
